package com.xiaohe.gulimall.seckill.controller;

import com.xiaohe.common.utils.R;
import com.xiaohe.gulimall.seckill.service.SeckillService;
import com.xiaohe.gulimall.seckill.to.SecKillSkuRedisTO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus(){

        List<SecKillSkuRedisTO> vos = seckillService.getCurrentSeckillSkus();


        return R.ok().setData(vos);
    }

    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R skuSkuSeckillInfo(@PathVariable("skuId") Long skuId){

        SecKillSkuRedisTO to = seckillService.skuSkuSeckillInfo(skuId);
        return R.ok().setData(to);
    }

    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId,
                     @RequestParam("key") String key,
                     @RequestParam("num") Integer num,
                     Model model){

        //1、判断是否登陆
        String orderSn =seckillService.kill(killId,key,num);
        model.addAttribute("orderSn",orderSn);
        return "success";

    }
}
